<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
      <el-form :inline="true" :model="searchParams">
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input v-model="searchParams.enterpriseName" placeholder="请选择企业名称" readonly="readonly">
            <i slot="suffix" class="el-input__icon el-icon-search" @click="findEnterprise"></i>
          </el-input>
        </el-form-item>
        <el-form-item label="部门名称">
            <el-input v-model="searchParams.departmentName" placeholder="请输入部门名称" maxlength="64"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table
        :data="pageData.list"
        border
        v-loading="pageData.loading"
        size="mini"
        header-cell-class-name="table-header-cell-font"
        cell-class-name="table-cell-font"
        @row-dblclick="departmentSelected">
        <el-table-column prop="enterpriseName" label="企业名称"></el-table-column>
        <el-table-column prop="departmentName" label="部门名称"></el-table-column>
        <el-table-column label="部门描述">
          <template v-slot="{ row }">
            <div class="ellipsis">
              {{ row.departmentDescription }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="parentDepartmentName" label="上级部门名称"></el-table-column>
      </el-table>
      <div class="table-pagination">
        <el-pagination
          @current-change="paginationChange"
          :current-page="pageData.current"
          :page-sizes="pageData.pageSizes"
          :page-size="pageData.size"
          background
          layout="total, prev, pager, next, jumper"
          :total="pageData.count">
        </el-pagination>
      </div>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="11" :offset="6">
          <div class="form-button-container">
            <el-button type="primary" @click="onBack">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-dialog :visible.sync="enterpriseSelectorVisible" v-if="enterpriseSelectorVisible" title="企业信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <enterprise-selector @enterprise-selected-event="handleEnterpriseSelectedEvent">
      </enterprise-selector>
    </el-dialog>
  </div>
</template>

<script>
import departmentApi from '@/api/departmentApi'
import EnterpriseSelector from "@/views/enterprise/EnterpriseSelector.vue";
import listQueryMixin from '@/mixins/listQueryMixin'

export default {
  name: 'DepartmentSelector',
  components: {EnterpriseSelector,},
  mixins: [
    listQueryMixin
  ],
  data () {
    return {
      searchParams: {
        enterpriseId: '',
        enterpriseName: '',
        departmentName: ''
      },
      enterpriseSelectorVisible: false,
      departmentSelectorVisible: false,
    }
  },
  methods: {
    getPageData (p) {
      return departmentApi.find(p)
    },
    onSearch () {
      this._getPageData()
    },
    onReset () {
      this.searchParams = {
        enterpriseId: '',
        enterpriseName: '',
        departmentName: '',
        parentDepartmentId: '',
        parentDepartmentName: '',
      }
      this.pageData.pageIndex = 1
      this._getPageData()
    },
    departmentSelected (row) {
      this.$emit('department-selected-event', {
        parentDepartmentId: row.id,
        departmentId: row.id,
        parentDepartmentName: row.departmentName,
        departmentName: row.departmentName,
      })
    },
    findEnterprise () {
      this.enterpriseSelectorVisible = true
    },
    handleEnterpriseSelectedEvent (selectedEnterprise) {
      this.enterpriseSelectorVisible = false
      if (selectedEnterprise && 'enterpriseId' in selectedEnterprise && this.searchParams.enterpriseId !== selectedEnterprise['enterpriseId']) {
        this.searchParams.enterpriseId = selectedEnterprise['enterpriseId']
        this.searchParams.enterpriseName = selectedEnterprise['enterpriseName']
      }
    },
    onBack () {
      this.$emit('department-selected-event')
    }
  }
}
</script>

<style lang="scss">
</style>
